import { message, Modal } from 'ant-design-vue';
export function debounce(fn, delay) {
  var timer;
  return function () {
    var context = this;
    var args = arguments;
    clearTimeout(timer);
    timer = setTimeout(function () {
      fn.apply(context, args);
    }, delay);
  };
}
export function LoadingControl({ call, params, change }) {
  change({
    status: 'loading',
    loading: true
  })
  return call(params)
    .then((res) => {
      change({
        status: 'success',
        loading: false,
        data: res
      })
      return res
    })
    .catch((e) => {
      change({
        status: 'error',
        loading: false
      })
      return Promise.reject(e)
    })
}
export function canLinkNode(nodes, self) {
  const linkMap = {
    // a:['b'],
    // b:['c']
  }
  nodes.forEach((node) => {
    const {
      nid,
      props: { behaviorLinkage = [] },
    } = node
    linkMap[nid] = linkMap[nid] || []
    behaviorLinkage.forEach((item) => {
      const { targets } = item
      targets.forEach((i) => {
        linkMap[nid].push(i.fieldId)
      })
    })
  })
  /*
    //2
    {a:['b'],b:['c'],c:['d'],d:[]}
    转成
    {"a":["b","c","d"],"b":["c","d"],"c":["d"],"d":[]}
    */
  for (const key in linkMap) {
    function add(v) {
      if (!v) {
        return
      }
      if (Array.isArray(v)) {
        linkMap[key] = [...new Set(linkMap[key].concat(v))]
        v.forEach((item) => {
          add(linkMap[item])
        })
      } else {
        linkMap[key].push(v)
        add(v)
        add(linkMap[v])
      }
    }
    linkMap[key].forEach((nid) => {
      add(linkMap[nid])
    })
  }
  /*
    step 3
    {"a":["b","c","d"],"b":["c","d"],"c":["d"],"d":[]}
    转成
    {
        b:[a],
        c:[a,b],
        d:[a,b,c]
    }
    */
  const parents = {}
  for (parent in linkMap) {
    const children = linkMap[parent]
    children.forEach((child) => {
      parents[child] = parents[child] || []
      parents[child].push(parent)
    })
  }
  // console.log(parents[self.nid], 'parents[self.nid]')
  if (parents[self.nid]) {
    let nodelist = []
    parents[self.nid].forEach((pnid) => {
      const linkNodes = nodes.filter((nodesNid) => {
        return pnid === nodesNid.nid
      })
      nodelist = nodelist.concat(linkNodes)
      console.log(linkNodes, 'linkNodes')
    })
    const list = []
    nodelist.map((node) => {
      const {
        nid,
        props: { behaviorLinkage = [] },
      } = node
      linkMap[nid] = linkMap[nid] || []
      behaviorLinkage.forEach((item) => {
        const { targets } = item
        targets.forEach((i) => {
          if (i.fieldId === self.nid) {
            console.log(nid, item, '=========')
            list.push({
              nid: nid,
              value: item.value,
            })
          }
        })
      })
    })
    let islist = []
    // console.log(list,'list')
    list.forEach((item) => {
      let nidval = nodes.filter((node) => {
        console.log(item, 'node')
        return node.nid === item.nid
      })
      if (nidval) {
        if (nidval[0].value === item.value) {
          islist.push('true')
        } else {
          islist.push('false')
        }
      }
    })
    const linkIsShow = islist.filter((list) => {
      return list === 'false'
    })
    if (linkIsShow.length > 0) {
      return false
    } else {
      return true
    }
    // console.log(list, 'list')
  } else {
    // console.log('11')
    return true
  }
}
const RULES = {
  REQUIRED({ value, error }) {
    return {
      result: !!value,
      msg: window.vueInstance.$t('hint.pleaseEnter', [error.toLowerCase()])
    }
  }
}
export const Tip = {
  success: message.success,
  error: message.error,
  verify(params) {
    let fields
    if (Array.isArray(params)) {
      fields = params
    } else {
      fields = params.list
    }
    for (let i = 0; i < fields.length; i++) {
      const { value, rules = [RULES.REQUIRED], error } = fields[i]
      let cache
      const result = rules.every(call => {
        cache = call({ value, error })
        return cache.result
      })
      if (!result) {
        Tip.error(cache.msg)
        return Promise.reject(cache)
      }
    }
    return Promise.resolve()
  },
  request({ call, params, title, content, confirmText, cancelText }) {
    const { vueInstance } = window
    return new Promise((resolve, reject) => {
      Modal.confirm({
        title,
        content,
        confirmText,
        cancelText,
        onOk() {
          return call(params)
            .then(resolve)
            .catch(reject)
        }
      });
    })
  }
}
export function formatSelect(list, config) {
  const { label, value, children } = config
  return list.map(node => {
    const c = Array.isArray(node[children]) ? formatSelect(node[children], config) : null
    if (c) {
      delete node[children]
      return {
        ...node,
        label: node[label],
        value: node[value],
        children: c
      }
    }
    return {
      ...node,
      label: node[label],
      value: node[value]
    }
  })
}
export const TOKEN = {
  encode(token) {
    return encodeURIComponent(escape(token));
  },
  decode(token) {
    return decodeURIComponent(unescape(token));
  }
};